package main.java.零钱通.Servies;


import main.java.房屋租借系统.JavaBeen.User;
import main.java.零钱通.tool.moneytool;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Scanner;

/**
 * @Auther:Liu
 * @Testname:moneyServies
 * @Date:2025/3/21 11:38
 */
public class moneyServies {
    public void Mains() throws Exception {
        boolean falge = true;
        Scanner scanner = new Scanner(System.in);
        moneytool moneytool = new moneytool();

        System.out.println("-----------登录系统------------");
        System.out.println("请输入username:");
        String admire = scanner.next();
        System.out.println("请输入password:");
        String password = scanner.next();
        User search = moneytool.load(admire, password);
        String str = moneytool.Ma();
        System.out.println("验证码如下:" + str);
        System.out.println("请输入验证码:");
        String str1 = moneytool.readString(5);
        if (search != null && str.equals(str1)) {
            System.out.println("登陆成功");
            int key = 0;

            double balance = 0.00;
            if (moneytool.selectone() != null) {
                balance = moneytool.selectone().getBalance();
            } else {
                balance = 0.0;
            }
            do {
                scanner = new Scanner(System.in);
                System.out.println("-------零钱通菜单-------");
                System.out.println("1  零  钱  通  明  细");
                System.out.println("2  收  益      入  账");
                System.out.println("3  消             费");
                System.out.println("4  退             出");
                System.out.println("请输入你的选择(1~4):");
                key = moneytool.readInt(1);
                switch (key) {
                    case 1:
                        System.out.println("-------零钱通明细-------");
                        List<main.java.零钱通.JavaBeen.User> users = moneytool.selectAll();
                        System.out.println("ID" + "  " + "项目" + "  " + "金额" + "  " + "时间" + "  " + "余额");
                        for (int i = 0; i < users.size(); i++) {
                            System.out.println(users.get(i).getProject() + "  " + users.get(i).getAmount() + "  " + users.get(i).getTime() + "  " + users.get(i).getBalance());
                        }
                        break;
                    case 2:
                        System.out.println("-------收益入账-------");
                        main.java.零钱通.JavaBeen.User user = new main.java.零钱通.JavaBeen.User();
                        System.out.println("请输入项目:");
                        String Project = main.java.零钱通.tool.moneytool.readString(5);
                        user.setProject(Project);
                        System.out.println("请输入金额:");
                        double Amount = scanner.nextDouble();
                        user.setAmount(Amount);

                        Date date = new Date();
                        user.setTime(date);


                        if (Amount > 0) {

                            balance += Amount;
                            user.setBalance(balance);
                            int insert = moneytool.insert(user);
                            if (insert == 1) {
                                System.out.println("入账成功!");
                                break;
                            }
                        } else {
                            System.out.println("入账不能为负数!");
                            break;
                        }

                    case 3:
                        System.out.println("-------消费-------");
                        main.java.零钱通.JavaBeen.User user1 = new main.java.零钱通.JavaBeen.User();
                        System.out.println("请输入项目:");
                        String Project1 = main.java.零钱通.tool.moneytool.readString(5);
                        user1.setProject(Project1);
                        System.out.println("请输入金额:");
                        double Amount1 = scanner.nextDouble();
                        user1.setAmount(Amount1);

                        Date date1 = new Date();
                        user1.setTime(date1);

                        if (balance < Amount1) {
                            System.out.println("消费失败!余额不足!");
                            break;
                        } else {
                            if (Amount1 > 0) {
                                balance -= Amount1;
                                user1.setBalance(balance);
                                int insert1 = moneytool.insert(user1);
                                if (insert1 == 1) {
                                    System.out.println("消费成功!");
                                    break;
                                }
                            } else {
                                balance += Amount1;
                                user1.setBalance(balance);
                                int insert1 = moneytool.insert(user1);
                                if (insert1 == 1) {
                                    System.out.println("消费成功!");
                                    break;
                                }
                            }
                        }
                        break;
                    case 4:
                        falge = false;
                        break;
                }
            } while (falge);
        } else {
            System.out.println("登录失败!");
            moneytool.broke();
        }
    }
}
